#!/bin/bash

#  This file is part of Urd.
#
#  Urd is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#  Urd is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program. See the file "COPYING". If it does not
#  exist, see <http://www.gnu.org/licenses/>.
#
# $LastChangedDate: 2008-03-18 17:16:06 +0100 (Tue, 18 Mar 2008) $
# $Rev: 1030 $
# $Author: styck $
# $Id: install.sh 1030 2008-03-18 16:16:06Z styck $
#

#Actions:
# We assume that the source files have been installed, and this script
#  will stop URDD, update the database, install extra required packages,
#  and ultimately start URDD back up.

# For updating the database, we need to know the old version number.
# This can be done in 2 ways:
# - Check for an 'oldversion' file, pre-created by apt-get upgrade script
# - Check the database

pidfile="../urdd.pid"
urdd_daemon="urdd.php"
urd_basedir="../"

# Determine the database type
dbtype=`grep databasetype ../dbconfig.php |  cut -s -d\' -f 4`
dbname=`grep \'database\' ../dbconfig.php |  cut -s -d\' -f 4`
dbuser=`grep db_user ../dbconfig.php |  cut -s -d\' -f 4`
dbpass=`grep db_password ../dbconfig.php |  cut -s -d\' -f 4`
dbhost=`grep db_hostname ../dbconfig.php |  cut -s -d\' -f 4`


mysql_cmd="mysql --user=$dbuser --password=$dbpass $dbname --max_allowed_packet=16M --host=$dbhost"
psql_cmd="psql -v ON_ERROR_STOP=1 -q -U $dbuser -d $dbname --host=$dbhost"


if [ "$dbtype" = "postgres8" -o "$dbtype" = "postgres7" ] ; then
	dbtype="pgsql"
elif [ "$dbtype" = "mysql" -o "$dbtype" = "mysqli" -o "$dbtype" = "mysqlt" ] ; then
	dbtype="mysql"
else
	echo "unknown database type $dbtype"
	exit -1
fi



# Store the password in case of pgsql:
if [ "$dbtype" = "pgsql" ] ; then
	export PGPASSWORD=$dbpass
fi

# STOP URDD

if [ -r $pidfile ] ; then
	pid=`cat $pidfile`
	rm $pidfile
elif [ -x "/usr/bin/pgrep" ] ; then
	pid=`pgrep -f $urdd_daemon`
fi 
if [ -z "$pid"  -a -x "/usr/bin/cut" ] ; then 
	pid=`ps ax|grep $urdd_daemon|grep -v grep | cut -f 1 -d ' '`
fi 
if [ -n "$pid" ] ; then 

	kill $pid 2> /dev/null
	kill -9 $pid 2> /dev/null

	pid=`ps ax|grep $urdd_daemon|grep -v grep | cut -f 1 -d ' '`
	if [ -n "$pid" ] ; then
		echo "Could not kill the URD daemon process"
		echo "Please stop it manually"
		if [ "$(id -u)" != "0" ]; then
			echo "Or run this script as the root user"
		fi
		exit -1
	fi

fi


# First see if we get the old version in the parameter:
if [ "${#}" = 1 ] ; then
	oldversion=$1
else

	oldversion="0"

	# Check if version.php exists, needed for <0.6.2 versions where the database value may be incorrect.
	if [ -f ../functions/version.php ] ; then
		oldversion=`grep '$urd_version =' ../functions/version.php |  awk -F"'" '{print $2 }'`
	else
		#Mysql:
		if [ "$dbtype" = "mysql" ] ; then
			oldversion=`$mysql_cmd -e 'select value from preferences where \`option\` = "URD_version";' -ss`
			if [ "$?" != "0" ] ; then
				echo "Query could not be executed"
				exit -1;
			fi
		#Pgsql:
		elif [ "$dbtype" = "pgsql" ] ; then
			qry="select \"value\" from preferences where \"option\" = 'URD_version';"
			oldversion=`$psql_cmd -t -c "$qry"`
			if [ "$?" != "0" ] ; then
				echo "Query could not be executed"
				exit -1;
			fi
		fi
	fi
	oldversion=`echo $oldversion | awk '{sub("^ ", "", $0); print $0}'`

	if [ "$oldversion" = "0" ] ; then
		echo "Sorry, I seem to be unable to determine your current version. Please run this script with your current version as the parameter (e.g. ./update.sh 0.5.5)"
		exit -1
	fi
fi

# Sanity checks:
if [ -z "$dbtype" ] ; then dbtype=0; fi
if [ -z "$dbname" ] ; then dbname=0; fi
if [ -z "$dbuser" ] ; then dbuser=0; fi
if [ -z "$dbpass" ] ; then dbpass=0; fi
if [ -z "$dbhost" ] ; then dbhost=0; fi


#Debug stuff:
#echo "database info $dbtype $dbname $dbuser $dbpass $dbhost $oldversion"
#exit

# Update process:

if [ "$oldversion" \< "0.5.5" ] ; then
	echo "Sorry, your version is too old for automatic upgrading. Please remove the current version and install the latest one."
	exit -1
fi

if [ "$oldversion" \< "0.6.0" ] ; then
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_0.5.5_to_0.6.0_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
	# zoiets
		$psql_cmd update_db_0.5.5_to_0.6.0_pgsql.sql
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi

if [ "$oldversion" \< "0.6.1" ] ; then
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_0.6.0_to_0.6.1_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_0.6.0_to_0.6.1_pgsql.sql
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi

fi

if [ "$oldversion" \< "0.6.2" ] ; then
	# Remove files:
	rm -f ../config.php.default
	rm -f ../install.1.inc.php
	rm -f ../install.2.inc.php
	rm -f ../install.3.inc.php
	rm -f ../install.4.inc.php
	rm -f ../install.5.inc.php
	rm -f ../install.6.inc.php
	rm -f ../install.bsd.sh
	rm -f ../install.funcs.inc.php
	rm -f ../install.php
	rm -f ../install.redhat.sh
	rm -f ../install.sh
	rm -f ../install.suse.sh
	rm -f ../install.ubuntu.sh
	rm -f ../update_db_0.5.5_to_0.6.0_mysql.sql
	rm -f ../update_db_0.6.0_to_0.6.1_mysql.sql
	rm -f ../update_db_0.6.0_to_0.6.1_pgsql.sql
	rm -f ../URD_mysql_db.sql
	rm -f ../URD_pgsql_db.sql
	rm -f ../functions/version.php

	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_0.6.1_to_0.6.2_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_0.6.1_to_0.6.2_pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi

if [ "$oldversion" \< "1.0.0" ] ; then
	# Remove files:
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_0.6.2_to_1.0.0_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_0.6.2_to_1.0.0_pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi


if [ "$oldversion" \< "1.0.1" ] ; then
	# Remove files:
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_1.0.0_to_1.0.1_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_1.0.0_to_1.0.1_pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi

if [ "$oldversion" \< "1.0.2" ] ; then
	# Remove files:
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_1.0.1_to_1.0.2_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_1.0.1_to_1.0.2_pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi

if [ "$oldversion" \< "1.0.3" ] ; then
	# Remove files:
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_1.0.2_to_1.0.3_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_1.0.2_to_1.0.3.pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi


if [ "$oldversion" \< "1.0.4" ] ; then
	# Remove files:
	# Update DB
	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_1.0.3_to_1.0.4_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
			exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_1.0.3_to_1.0.4.pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi

if [ "$oldversion" \< "1.0.5" ] ; then
	# Remove files:
	# Update DB
    php -f update_db_1.0.4_to_1.0.5.php

	if [ "$dbtype" = "mysql" ] ; then
		$mysql_cmd < update_db_1.0.4_to_1.0.5_mysql.sql
		if [ "$?" != "0" ] ; then
			echo "Query could not be executed"
		exit -1;
		fi
	fi
	if [ "$dbtype" = "pgsql" ] ; then
		$psql_cmd update_db_1.0.4_to_1.0.5.pgsql.sql 
		if [ "$?" != "0" ] ; then
			echo "Database update failed"
			exit -1;
		fi
	fi
fi

# just in case
unset PGPASSWORD

echo "URDD has been upgraded to the latest version."
exit 0
